package com.jwl.idc.database;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;

public class JwlContentProvider extends ContentProvider {

	private MyDatabaseHelper myDatabaseHelper;
	private SQLiteDatabase db;
	private ContentResolver cr;

	private static final UriMatcher sURIMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);

	private static final int INDEX_BASE = 0;
	private static final int USER_INFO = INDEX_BASE + 1;


	static {
		sURIMatcher.addURI(ContactDBConstant.AUTHORITY, "userInfo", USER_INFO);

	}
	@Override
	public int delete(Uri uri, String where, String[] arg) {
		// TODO Auto-generated method stub
		if (db == null)
			db = myDatabaseHelper.getWritableDatabase();
		if (cr == null)
			cr = getContext().getContentResolver();
		int count = 0;
		switch (sURIMatcher.match(uri)) {
		case USER_INFO:
			count = db.delete(ContactDBConstant.TABLE_NAMES[0], where, arg);
			cr.notifyChange(uri, null);
			break;
		default:
			throw new IllegalArgumentException("unknow url  = " + uri);
		}
		return count;
	}
	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Uri insert(Uri uri, ContentValues cv) {
		if (db == null)
			db = myDatabaseHelper.getWritableDatabase();
		if (cr == null)
			cr = getContext().getContentResolver();
		long count = 0l;
		Uri rUri;
		switch (sURIMatcher.match(uri)) {
		case USER_INFO:
			count = db.insert(ContactDBConstant.TABLE_NAMES[0],
					ContactDBConstant.USER_INFO_ID, cv);
			if (count > 0) {
				rUri = ContentUris.withAppendedId(
						ContactDBConstant.USERINFO_URI, count);
				cr.notifyChange(uri, null);
			} else {
				new SQLiteException("sqlite is error about : " + uri);
				rUri = null;
			}
			break;
		default:
			new SQLiteException("sqlite is error about : " + uri);
			rUri = null;
		}
		return rUri;
	}
	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		final Context context = getContext();
		myDatabaseHelper = new MyDatabaseHelper(context);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] columns, String selection,
			String[] selectionArgs, String orderBy) {
		if (db == null)
			db = myDatabaseHelper.getReadableDatabase();
		if (cr == null)
			cr = getContext().getContentResolver();
		Cursor cursor;
		switch (sURIMatcher.match(uri)) {
		case USER_INFO:
			cursor = db.query(ContactDBConstant.TABLE_NAMES[0], columns,
					selection, selectionArgs, null, null, orderBy);
			cursor.setNotificationUri(cr, uri);
			break;
		default:
			new SQLiteException("sqlite is error about : " + uri);
			cursor = null;
		}
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String whereClause,
			String[] whereArgs) {
		// TODO Auto-generated method stub
		if (db == null)
			db = myDatabaseHelper.getWritableDatabase();
		if (cr == null)
			cr = getContext().getContentResolver();
		int count;
		switch (sURIMatcher.match(uri)) {
		case USER_INFO:
			count = db.update(ContactDBConstant.TABLE_NAMES[0], values,
					whereClause, whereArgs);
			cr.notifyChange(uri, null);
			break;
		default:
			new SQLiteException("update is error about " + uri);
			count = -1;
		}
		return count;
	}

}
